package com.bootcamp.mall.service.admin;

import com.bootcamp.mall.dao.AdminDao;
import com.bootcamp.mall.model.Admin;
import com.bootcamp.mall.model.bo.admin.AddAdminBO;
import com.bootcamp.mall.model.bo.admin.AdminLoginInfoBO;
import com.bootcamp.mall.model.bo.admin.SearchAdminBO;
import com.bootcamp.mall.model.bo.admin.UpdateAdminBO;
import com.bootcamp.mall.model.bo.admin.ChangeAdminPwdBO;
import com.bootcamp.mall.model.vo.admin.AdminLoginInfoVO;
import com.bootcamp.mall.model.vo.admin.AllAdminInfoVO;
import com.bootcamp.mall.utils.MyBatisUtil;
import com.bootcamp.mall.utils.StringUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: mall
 * @description: implements admin service
 * @author: Keyu Li
 * @create: 2021-11-09 09:15
 **/

public class AdminServiceImpl implements AdminService {
    @Override
    public AdminLoginInfoVO login(AdminLoginInfoBO adminBO) {
        SqlSession sqlsession = MyBatisUtil.getSqlsession();
        AdminDao mapper = sqlsession.getMapper(AdminDao.class);
        // 把bo封装为pojo进行数据库查询
        Admin admin = new Admin();
        admin.setUsername(adminBO.getEmail());
        admin.setPassword(adminBO.getPwd());
        AdminLoginInfoVO adminVO = mapper.selectAdminByEmailAndPass(admin);
        sqlsession.close();
        return adminVO;
    }

    @Override
    public List<AllAdminInfoVO> allAdmins() {
        SqlSession sqlsession = MyBatisUtil.getSqlsession();
        AdminDao mapper = sqlsession.getMapper(AdminDao.class);
        List<Admin> admins = mapper.selectAllAdmins();
        if (admins == null) {
            return null;
        }
        // 把pojo对象封装为vo 返回
        List<AllAdminInfoVO> adminVOs = new ArrayList<>();
        for (Admin admin : admins) {
            AllAdminInfoVO adminVO = new AllAdminInfoVO();
            adminVO.setId(admin.getId());
            adminVO.setEmail(admin.getUsername());
            adminVO.setNickname(admin.getNickname());
            adminVO.setPwd(admin.getPassword());
            adminVOs.add(adminVO);
        }
        return adminVOs;
    }

    @Override
    public Integer addAdmins(AddAdminBO adminBO) {
        SqlSession sqlsession = MyBatisUtil.getSqlsession();
        AdminDao mapper = sqlsession.getMapper(AdminDao.class);
        Admin admin = new Admin();
        admin.setId(null);
        admin.setUsername(adminBO.getEmail());
        admin.setNickname(adminBO.getNickname());
        admin.setPassword(adminBO.getPwd());
        // 捕获异常插入异常，同名username时候返回 0
        try {
            int affectRows = mapper.insertAdmins(admin);
            System.out.println(affectRows);
            sqlsession.commit();
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            sqlsession.rollback();
            return -1;
        } finally {
            sqlsession.close();
        }
    }

    @Override
    public AllAdminInfoVO getAdminsInfo(Integer id) {
        SqlSession sqlsession = MyBatisUtil.getSqlsession();
        AdminDao mapper = sqlsession.getMapper(AdminDao.class);
        // 从id得到pojo，再封装为vo返回
        Admin admin = mapper.selectAdminsById(id);
        if (admin == null) {
            return null;
        }
        AllAdminInfoVO adminVO = new AllAdminInfoVO();
        adminVO.setId(id);
        adminVO.setEmail(admin.getUsername());
        adminVO.setNickname(admin.getNickname());
        adminVO.setPwd(admin.getPassword());
        // close
        sqlsession.close();
        return adminVO;
    }

    @Override
    public Integer updateAdminss(UpdateAdminBO adminBO) {
        SqlSession sqlsession = MyBatisUtil.getSqlsession();
        AdminDao mapper = sqlsession.getMapper(AdminDao.class);
        // bo对象转换为pojo进行查询
        Admin admin = new Admin();
        admin.setId(adminBO.getId());
        admin.setUsername(adminBO.getEmail());
        admin.setNickname(adminBO.getNickname());
        admin.setPassword(adminBO.getPwd());
        // 返回0表示成功，为 -1 表明修改失败
        try {
            int affectRows = mapper.updateInfoById(admin);
            System.out.println(affectRows);
            // commit
            sqlsession.commit();
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            sqlsession.rollback();
            return -1;
        } finally {
            sqlsession.close();
        }
    }

    @Override
    public Integer deleteAdmins(Integer id) {
        SqlSession sqlsession = MyBatisUtil.getSqlsession();
        AdminDao mapper = sqlsession.getMapper(AdminDao.class);
        // 先查询判断一下是否删除的是 admin
        Admin admin = mapper.selectAdminsById(id);
        if ("admin".equals(admin.getUsername())) {
            return -1;
        }
        // 删除该id对应用户
        Integer affectRows = mapper.deleteAdminsById(id);
        // commit and close
        sqlsession.commit();
        sqlsession.close();
        return affectRows;
    }

    @Override
    public List<AllAdminInfoVO> getSearchAdmins(SearchAdminBO adminBO) {
        SqlSession sqlsession = MyBatisUtil.getSqlsession();
        AdminDao mapper = sqlsession.getMapper(AdminDao.class);
        // 根据username和nickname查询
        String username = "%" + adminBO.getEmail() + "%";
        String nickname = "%" + adminBO.getNickname() + "%";
        List<Admin> admins = mapper.selectAdminsByUsernameAndNickname(username, nickname);
        // close
        sqlsession.close();
        // 如果查询结果为null，返回null
        if (admins == null) {
            return null;
        }
        // pojo list 封装为 vo list
        List<AllAdminInfoVO> adminVOs = new ArrayList<>();
        for (Admin admin : admins) {
            AllAdminInfoVO adminVO = new AllAdminInfoVO();
            adminVO.setId(admin.getId());
            adminVO.setEmail(admin.getUsername());
            adminVO.setNickname(admin.getNickname());
            adminVO.setPwd(admin.getPassword());
            adminVOs.add(adminVO);
        }

        return adminVOs;
    }

    @Override
    public Integer changePwd(ChangeAdminPwdBO adminBO) {
        // 判断当前用户是否是 admin，禁止 admin 修改密码
        if ("admin".equals(adminBO.getAdminToken())) {
            return 402;
        }
        // 校验两次输入密码是否一致
        if (!adminBO.getNewPwd().equals(adminBO.getConfirmPwd())) {
            return 400;
        }
        // 对密码进行正则校验，确保合法性 —— 只校验不为空
        if (StringUtil.isEmpty(adminBO.getNewPwd())) {
            return 401;
        }
        // 更改用户密码
        SqlSession sqlsession = MyBatisUtil.getSqlsession();
        AdminDao mapper = sqlsession.getMapper(AdminDao.class);
        Integer affectRows = mapper.updatePwdByUsername(adminBO);
        // commit and close
        sqlsession.commit();
        sqlsession.close();
        return affectRows;
    }
}
